#include <stdio.h>
#include <stdlib.h>

typedef struct BNode{
    BNode * lchild;
    BNode * rchild;
    int data;
}BNode,*BTree;

// 获取指定值的结点的所有祖先结点
bool getParents(BTree T, int x) {
    if(!T)
        return false;
    if(T->data == x)
        return true;
    if (getParents(T->lchild,x) || getParents(T->rchild,x))
    {
        printf("%d ",T->data);
        return true;
    }
    return false;
}


// 测试
void print(BTree T) {
    if (T) {
        printf("%d ",T->data);
        print(T->lchild);
        print(T->rchild);
    }
}

BNode * GetNode(int x) {
    BNode *node = (BNode *)malloc(sizeof(BNode));
    node->data = x;
    node->lchild = NULL;
    node->rchild = NULL;
}

int main() {
    BTree T = GetNode(1);
    T->lchild = GetNode(2);
    T->rchild = GetNode(3);
    T->lchild->lchild = GetNode(4);
    T->rchild->rchild = GetNode(5);

    print(T);
    printf("\nparent nodes:");
    getParents(T, 5);
    return 0;
}